<?php
/*
 * Aclass_option 选项配置
 * 2012-05-06
 * abans@qq.com
 */
class Aclass_option{
	function  __construct($key='') {
		if($key!=''){
			$this->ready($key);
		}
	}
	function ready($key){
		$this->key = $key;
		$this->ckey = Acan_app_id.':option_'.$key;
		$obj = Afun_cache_get($this->ckey);
		if($obj==false && strlen($obj->value)==0){
			$model = Afun_db()->model(Acan_table_option);
			$this->obj = $model->ofind("`key`='$this->key' and `appid`=".Acan_app_id);
			if(!$this->obj){
				$this->obj->value = false;
			}
			Afun_cache_set($this->ckey,$this->obj);
		}else{
			$this->obj = $obj;
		}
	}
	function get($key=''){
		if($key!=''){
			$this->ready($key);
		}
		return $this->obj->value;
	}
	function set($str,$title=''){
		if(is_object($str) || is_array($str)){
			$str = Ajson_encode($str);
		}
		if(!empty($title)){
			$save['title'] = $title;
		}
		$save['key'] = $this->key;
		$save['value'] = $str;
		$save['appid'] = Acan_app_id;
		$model = Afun_db()->model(Acan_table_option);
		if(!empty($this->obj)){
			$save['id'] = $this->obj->id;
		}
		$model->save($save);
		Afun_cache_rm($this->ckey);
		return true;
	}
	function del(){
		if(!empty($this->obj)){
			Afun_db()->model(Acan_table_option)->deletePk($this->obj->id);
		}
		Afun_cache_rm($this->ckey);
		return true;
	}
	function delcache(){
		Afun_cache_rm($this->ckey);
		return true;
	}
}
?>
